<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>信息填报</title>
<#include "/common/resource.ftl">
<#include "/common/upload.ftl">
<script type="text/javascript" charset="utf-8" src="${params.contextPath}/common/ueditor/ueditor.config.js"></script>
	<script type="text/javascript" charset="utf-8" src="${params.contextPath}/common/ueditor/ueditor.all.js"></script>
	<script type="text/javascript" charset="utf-8" src="${params.contextPath}/common/ueditor/lang/zh-cn/zh-cn.js"></script>
    <style type="text/css">
    .panel-header{background: #fafafa;}
    .informationTemplate{border: 1px solid #ddd;height: 28px;}
    .informationContainer{padding-top: 10px;}

    </style>
    <script type="text/javascript">
        $(function () {
        <#if (params.id)??>
            $.ajaxRequest({
                url: '${params.contextPath}/web/archive/getById.json',
                data: {id: "${params.id}"},
                success: function (data) {
                    if (!data.success) {
                        $.message(data.message);
                        return;
                    }
                    var record = data.data;
                    for (var key in record) {
                        $("[name='" + key + "']").val(record[key]);
                    }
                    $("#categoryName").val(record.categoryName);
                    if(record.attachmentList){
                        $(record.attachmentList).each(function (i,item) {
                            var imgId = item.attachmentId;
                            var imgSize = item.size;
                            var imgUrl = "${params.contextPath}/web/attachment/download/{0}.json".format(imgId);
                            var content = '<div style="margin:5px 0px;">';
                            content += '<span><input type="hidden" value="{imageId}" name="fileIds"/></span>';
                            content += '<a href="{imageUrl}" target="_blank"><i class="AddFile AddBefImg">{imageName}({imageSize})</i></a>';
                            content +='<a onclick="$(this).parent().remove()">&nbsp;<font color="red">删除</font></a></div>';
                            content = content.format({imageId:imgId, imageUrl:imgUrl, imageName:item.originalName, imageSize:imgSize});
                            $("#select-file-button").before(content);
                        });
                    }
                }
            });
        </#if>
      //文件上传
   		$.upload({uploadSuccess:function(file, data){
				if (!data.success) {
					$.message(data.message);
					return;
				}
			var item = $.parseJSON(data._raw).data[0];
			var imgId = item.id;
			var imgSize = FileUtils.formatSize(file.size);
			var imgUrl = "${params.contextPath}/web/attachment/download/{0}.json".format(imgId);

			var content = '<div style="margin:5px 0px;" class="attachmentDiv">';
				content += '<span><input type="hidden" value="{imageId}" name="fileIds"/></span>';
                content += '<a href="{imageUrl}" target="_blank"><i class="AddFile AddBefImg">{imageName}({imageSize})</i></a>';
				content +='<a onclick="$(this).parent().remove()">&nbsp;<font color="red">删除</font></a></div>';

				content = content.format({imageId:imgId, imageUrl:imgUrl, imageName:item.name, imageSize:imgSize});
				$("#select-file-button").before(content);
   		}});
        });
    </script>
</head>
<body class="easyui-layout" fit="true" border="false">
    <div data-options="region:'east',split:true" title="基本信息" border="false" style="width:400px;">
    	<div class="ui-form">
		    <form class="layui-form my-form" action="${params.contextPath}/web/information/<#if (params.id)??>modify<#else>save</#if>.json" method="post">
		        <input type="hidden" name="id" value="${params.id}"/>
		        <input type="hidden" name="content" value=""/>
		        <input type="hidden" name="templateId" value=""/>
		        <div class="layui-form-item">
		            <label class="layui-form-label">信息标题<span class="ui-request">*</span></label>
		            <div class="layui-input-block">
		                <input type="text" class="layui-input" placeholder="请输入信息标题" name="title">
		            </div>
		        </div>
		        <div class="layui-form-item">
		            <label class="layui-form-label">信息编号<span class="ui-request">*</span></label>
		            <div class="layui-input-block">
		                <input type="text" class="layui-input" name="code" placeholder="请输入信息编号">
		            </div>
		        </div>
		        <div class="layui-form-item">
		            <label class="layui-form-label">签发人<span class="ui-request">*</span></label>
		            <div class="layui-input-block">
		                <input type="text" class="layui-input" name="signerName" placeholder="请输入签发人" value="${user.name}">
		            </div>
		        </div>
		        <div class="layui-form-item">
		            <label class="layui-form-label">签发单位<span class="ui-request">*</span></label>
		            <div class="layui-input-block">
		                <input type="text" class="layui-input" name="signOrgName" placeholder="请输入签发单位" value="${user.organization.name}">
		            </div>
		        </div>


		        <div class="layui-form-item">
		            <label class="layui-form-label">附件</label>
		            <div class="layui-input-block">
		                <a id="select-file-button">添加文件</a>
		            </div>
		        </div>
		        <div class="layui-form-item">
		            <div class="layui-input-block">
		                <input type="submit" value="保存并报送" class="layui-btn"/>

		            </div>
		        </div>
		    </form>
		</div>
    </div>
    <div data-options="region:'center',title:'信息正文'" border="false" style="padding: 10px">
    	<div style="line-height: 30px;border-bottom: 1px dashed #ddd;padding-bottom: 10px;">

   		<select id="informationTemplate" class="informationTemplate" onchange="templateChange();">
   			<option value="">---选择信息填报模板---</option>
   		</select>
   		</div>
        <div id="informationContainer" class="informationContainer">
            <script type="text/plain" id="myEditor" style="width:100%;height:450px;"></script>
        </div>
    </div>
</body>
<script type="text/javascript">
	var opts = {
		//定制工具按钮
		initialContent:"",
		autoClearinitialContent: false,//获取光标是，是否自动清空初始化数据
		elementPathEnabled : false,//是否展示元素路径
		wordCount:false,//是否计数
		autoHeightEnabled:false,//高度是否自动增长
		textarea:"contact_content"//后台接受UEditor的数据的参数名称
    };
	var ue = null;
    $(function(){
        initTemplate();
    	ue = UE.getEditor('myEditor',opts);
        $(".my-form").unbind().submit(function(){
            var content = ue.getContent();
            $("input[name='content']").val(content);
            var form = $(this);
            form.formSubmit({
                callBack:function (data) {
                    $("#informationTemplate").val("");
                    $("input[name='templateId']").val("");
                    $("input[name='content']").val("");
                    $(".attachmentDiv").remove();
                    ue.setContent("");
                }
            });
            return false;
        });
    });
    var templateContent = {};
    function initTemplate(){
        $.ajaxRequest({
            url:'${params.contextPath}/web/informationTemplate/list.json',
            success:function (resp) {
                if(!resp.success){
                    $.message(resp.message);
                }
                var templateList = resp.data;
                $(templateList).each(function (i, item) {
                    var opt = $("<option value='"+item.id+"'>"+item.name+"</option>");
                    $("#informationTemplate").append(opt);
                    templateContent[item.id] = item.content;
                });
            }
        });
    }
    function templateChange() {
        var templateId = $("#informationTemplate").val();
        var cfm = layer.confirm("更换信息模板会重置信息内容,确认更换吗?",function(){
            ue.setContent(templateContent[templateId]||"");
            $("input[name='templateId']").val(templateId);
            layer.close(cfm);
        });
    }
</script>
</html>
